/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package control
import entity.Address
import dao.AddresessDAO
import java.sql.Connection

/**
 *
 * @author Tato
 */
class ControlAddress {
    
    
    AddresessDAO dao = new AddresessDAO()
    
    
    def addAddress(Address addressP){
        
   
        if(validateAddress(addressP.address) == false){
                "Use entre 5 y 50 caracteres"
        }
         
         if(validateContactPosition(addressP.address) == false){
                "Use entre 3 y 30 caracteres"
        }
            
        if((validateAddress(addressP.address)) == true && validateContactPosition(addressP.address) == true){
            
            key = dao.create(addressP,DBConnection.establishConection())
            if(key > 0){
                   addressP.id = key
                   "Dirección Asiganda"
            }
            
            else {
                "Dirección no asignada"
            }
        }
    }
        
   
    def deleteAddress(Address address){
        
       
        delete = dao.remove(address,DBConnection.establishConection())
        
        if(delete == true){
            "Borrado exitoso"
        }
        else{
            "Borrado falló"
        }     
    }
    
    def updateAddress(Address address){
        
        
        if(dao.update(address,DBConnection.establishConection()) > 0){
               "Información actualizada"
            }
            
            else {
                "Información no actualizada"
            }
        
    }
    
     def findAddress(Integer id){
         
        
         if(dao.find(id,DBConnection.establishConection())!= null  ){
               "Información actualizada"
            }
            
            else {
                "Información no actualizada"
            }
        
    }
    
    
    
    def validateAddress(String addressP){
        if (addressP.length() >= 5&& addressP.length() <= 50)
            true
        
        false
    }
 /*   
    def validateType(String typeP){
        if(typeP==0 || typeP == 1 )
            true
        
        false
    }
   */
    
    def validateContactPosition(String contactP){
        if(contactP > 2 && contactP <= 30 )
            true
        
        false
    }
    
    
    
}
